﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.Productions;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.Strippings
{
    public class StrippingBackHandler : IRequestHandler<StrippingBackCommand, ResultsDTO<Stripping>>
    {
        #region 仓储
        private readonly IBateRepository<Stripping> StrippingRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="StrippingRepository"></param>
        public StrippingBackHandler(IBateRepository<Stripping> StrippingRepository)
        {
            this.StrippingRepository = StrippingRepository;
        }
        #endregion

        #region 生产退料 反填
        /// <summary>
        ///  生产退料 反填
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultsDTO<Stripping>> Handle(StrippingBackCommand request, CancellationToken cancellationToken)
        {
            //异常处理
            try
            {
                //初始化一个ResultDTO<Stripping>对象result，并设置Code为 0
                ResultsDTO<Stripping> result = new ResultsDTO<Stripping>() { Code = 0 };

                //连接mySql 
                string conn = "server=112.124.69.237;port=3306;uid=root;pwd=No6123666;database=MangoMesDB";

                //sql语句
                string str = $"select * from stripping where StrippingId = {request.StrippingId} ";

                Stripping stripping = new Stripping();
                //创建DapperHelper对象 用于与数据库进行交互
                DapperHelper helper = new DapperHelper();
                //调用StrippingRepository的GetAll方法 （）里面是执行查询并获取结果
                //就可以查询到的条件stripping数据存储在result对象中
                result.Objects = StrippingRepository.GetAll(str, stripping, conn).FirstOrDefault();
                result.Message = "生产退料 反填成功";

                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

    }
}
